<?php
namespace app\manage\controller;
use  think\facade\Db;
class Area extends Base
{

    public function getAreaList()
    {
        $list = Db::table('area')->field('id,name')->where([])->select()->toArray();
        return ajax(['list'=>$list]);
    }
    public function areaDataList()
    {
        $area = input('post.area','');
        $trade = input('post.trade','');
        $year = input('post.year','');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $where = [];
        if($area) {
            $where[] = ['area','=',$area];
        }
        if($trade) {
            $where[] = ['trade','=',$trade];
        }
        if($year) {
            $where[] = ['year','=',$year];
        }
        try {
            $count = Db::table('area_data')->where($where)->count();
            $list = Db::table('area_data')->where($where)
                ->order(['year'=>'ASC','id'=>'ASC'])
                ->limit(($curr_page - 1)*$perpage,$perpage)->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }

    public function areaDataDetail() {
        $post['id'] = input('post.id');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('area_data')->where($where)->find();
            if(!$exist) {
                return ajax('',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($exist);
    }

    public function saveData() {
        $post['year'] = input('post.year','');
        $post['trade'] = input('post.trade','');
        $post['area'] = input('post.area','');
        checkPost($post);
        $post['id'] = input('post.id','');
        $post['company_num'] = input('post.company_num',0);
        $post['month1'] = input('post.month1',0);
        $post['month1_last_year'] = input('post.month1_last_year',0);
        $post['month1_rate'] = input('post.month1_rate',0);
        $post['month2'] = input('post.month2',0);
        $post['month2_last_year'] = input('post.month2_last_year',0);
        $post['month2_rate'] = input('post.month2_rate',0);
        $post['month3'] = input('post.month3',0);
        $post['month3_last_year'] = input('post.month3_last_year',0);
        $post['month3_rate'] = input('post.month3_rate',0);
        $post['month4'] = input('post.month4',0);
        $post['month4_last_year'] = input('post.month4_last_year',0);
        $post['month4_rate'] = input('post.month4_rate',0);
        $post['month5'] = input('post.month5',0);
        $post['month5_last_year'] = input('post.month5_last_year',0);
        $post['month5_rate'] = input('post.month5_rate',0);
        $post['month6'] = input('post.month6',0);
        $post['month6_last_year'] = input('post.month6_last_year',0);
        $post['month6_rate'] = input('post.month6_rate',0);
        $post['month7'] = input('post.month7',0);
        $post['month7_last_year'] = input('post.month7_last_year',0);
        $post['month7_rate'] = input('post.month7_rate',0);
        $post['month8'] = input('post.month8',0);
        $post['month8_last_year'] = input('post.month8_last_year',0);
        $post['month8_rate'] = input('post.month8_rate',0);
        $post['month9'] = input('post.month9',0);
        $post['month9_last_year'] = input('post.month9_last_year',0);
        $post['month9_rate'] = input('post.month9_rate',0);
        $post['month10'] = input('post.month10',0);
        $post['month10_last_year'] = input('post.month10_last_year',0);
        $post['month10_rate'] = input('post.month10_rate',0);
        $post['month11'] = input('post.month11',0);
        $post['month11_last_year'] = input('post.month11_last_year',0);
        $post['month11_rate'] = input('post.month11_rate',0);
        $post['month12'] = input('post.month12',0);
        $post['month12_last_year'] = input('post.month12_last_year',0);
        $post['month12_rate'] = input('post.month12_rate',0);
        try {
            if($post['id']) {
                unset($post['year']);
                unset($post['trade']);
                unset($post['area']);
                Db::table('area_data')->where('id','=',$post['id'])->update($post);
            }else {
                $where = [
                    ['year','=',$post['year']],
                    ['trade','=',$post['trade']],
                    ['area','=',$post['area']],
                ];
                $exist = Db::table('area_data')->where($where)->find();
                if($exist) {
                    return ajax('',-1,'数据已存在!不可重复添加');
                }
                Db::table('area_data')->insertGetId($post);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }



}